﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;
using System.Windows.Interactivity;
using Microsoft.Expression.Interactivity.Core;
using System.Windows.Controls.Primitives;
//using Microsoft.Expression.Interactivity.Core;

namespace System8ToyMC
{
    /// <summary>
    /// When this fires, associated with a histogram, it will display a pop-up
    /// that shows the histogram in a much larger size.
    /// </summary>
	public class HistogramPopupBehavior : Behavior<Histogram>
	{
		public HistogramPopupBehavior()
		{
            ShowPopUp = new ActionCommand(DoPopupShow);
            HidePopUp = new ActionCommand(DoPopupHide);
		}

        /// <summary>
        /// We are attaching to the histogram object.
        /// </summary>
		protected override void OnAttached()
		{
			base.OnAttached();
		}

        /// <summary>
        /// We are detachign from teh histogram object
        /// </summary>
		protected override void OnDetaching()
		{
			base.OnDetaching();

			// Insert code that you would want run when the Behavior is removed from an object.
		}

        public ICommand ShowPopUp { get; private set; }
        public ICommand HidePopUp { get; private set; }

        /// <summary>
        /// The pop-up we are going to manage.
        /// </summary>
        private Popup _pop = null;

        /// <summary>
        /// Show the popup to everyone
        /// </summary>
        private void DoPopupShow()
        {
            if (_pop != null)
            {
                return;
            }

            PlotPopUp p = new PlotPopUp();
            p.Width = 800;
            p.Height = 600;
            _pop = new Popup();

            p.ClosePopUp += () => HidePopUp.Execute(null);

            p.SetHistogram(AssociatedObject);
            _pop.Child = p;
            _pop.HorizontalOffset = 20;
            _pop.VerticalOffset = 20;
            _pop.IsOpen = true;
        }

        /// <summary>
        /// Cleanly tear it down - and make sure there are no memory leaks! :-)
        /// </summary>
        private void DoPopupHide()
        {
            _pop.IsOpen = false;
            _pop = null;
        }
	}
}